﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.Memcached;
using Microsoft.AspNetCore.Mvc;

namespace MemcachedCoreSample.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly IMemcachedClient _memcachedClient;
        public ValuesController(IMemcachedClient memcachedClient)
        {
            _memcachedClient = memcachedClient;
        }
        // GET api/values
        [HttpGet]
        public async Task<IEnumerable<string>> GetAsync()
        {
            await _memcachedClient.StoreAsync(StoreMode.Set, "list", new string[] { "value1", "value2" }, TimeSpan.FromMinutes(3));
            var val = await _memcachedClient.GetValueAsync<List<string>>("list");
            //Thread.Sleep(3000);
            //val = await _memcachedClient.GetValueAsync<List<string>>("list");
            return val;
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
